package com.itany.nmms.service.proxy;

import com.itany.mvc.util.CommonsMultipartFile;
import com.itany.nmms.exception.FileUploadErrorException;
import com.itany.nmms.exception.ProductExistException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.ServiceException;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.ProductService;
import com.itany.nmms.tran.TransactionManager;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/29 10:53
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class ProductServiceProxy implements ProductService {
    @Override
    public void addProduct(String name, String price, List<CommonsMultipartFile> files, String typeId, HttpSession session) throws RequestParameterErrorException, ProductExistException, FileUploadErrorException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ProductService productService = (ProductService) ObjectFactory.getObject("productServiceTarget");

        try {
            tran.begin();
            productService.addProduct(name,price,files,typeId,session);
            tran.commit();
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (ProductExistException e) {
            tran.rollback();
            throw e;
        } catch (FileUploadErrorException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }

    }
}
